import { RESPONSE_CODE, STORE_KEY } from "@/common/config";
import { API_URL } from "@/common/serviveApi";
import { handleCommonUpload, handleCompressImg, handleLoginCallback } from "@/common/util";
import Taro from "@tarojs/taro";
import { useState } from "react";




const useUploadPhotoAction = () => {
    const [photoList, setPhotoList] = useState([])
    const [hasEditPhoto, setHasEditPhoto] = useState(false)

    
  
    const handleInitStorePhotoList = () => {
        const storePhotoList =JSON.parse( Taro.getStorageSync(STORE_KEY.PHOTO_LIST) || '[]')
       
        return storePhotoList
    }
    const handleInitStoreOriginPhotoList = () => {
        const storePhotoList =JSON.parse( Taro.getStorageSync(STORE_KEY.ORIGIN_PHOTO_LIST) || '[]')
       
        return storePhotoList
    }
    const handleBeforeUploadPhoto = (event) => {
        console.log(event)
    }
    const handleAfterUploadPhoto = async (event) => {

        const { file } = event.detail

        let fileTemp = [] as any
        const storePhotoList = handleInitStorePhotoList()
        handleCompressImg(file,20).then(async (compressedPaths) => {
            
            for (let i in compressedPaths) {
                
                const thumb = String(compressedPaths[i])

                let uploadVideoUrlRes = await handleCommonUpload(thumb);
                if (uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_EXPIRE || uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_NEED) {
                    handleLoginCallback(async () => {
                        uploadVideoUrlRes = await handleCommonUpload(thumb)
                    })
                }
                if (uploadVideoUrlRes.code === RESPONSE_CODE.SUCCESS) {
                    fileTemp.push(file[i])
                    setPhotoList(photoList.concat(fileTemp))
                    // setHasEditPhoto(true)

                    storePhotoList.push(uploadVideoUrlRes.fileUrl);
                    Taro.setStorageSync(STORE_KEY.PHOTO_LIST, JSON.stringify(storePhotoList))
                }


            }
        })
    }
    const handleDeletePhotoAction = (event) => {
        const { index } = event.detail
        const valueNew = JSON.parse(JSON.stringify(photoList))
        const storePhotoList = handleInitStorePhotoList()

        valueNew.splice(index, 1)
        storePhotoList.splice(index, 1)
        Taro.setStorageSync(STORE_KEY.PHOTO_LIST, JSON.stringify(storePhotoList))
        setPhotoList(valueNew)
        // setHasEditPhoto(true)
    }
    const handleFormatPhotoList = (storePhotoList) => {
        const storePhotoListT = storePhotoList.map((photo) => {
            const thumb = photo.thumb ? photo.thumb : photo
            const url = photo.url ? photo.url : photo

            return {
                type: 'image',
                size: 0,
                thumb: `${API_URL}${thumb}`,
                url: `${API_URL}${url}`,
            }
        });
        return storePhotoListT
    }
    return {
        photoList,
        hasEditPhoto,
        handleDeletePhotoAction,
        handleAfterUploadPhoto,
        handleInitStorePhotoList,
        handleInitStoreOriginPhotoList,
        setPhotoList,
        handleFormatPhotoList,
        handleBeforeUploadPhoto

    }

}
export default useUploadPhotoAction;